package dao.impl;

import bean.Athlete;
import bean.Competition;
import dao.CompetitionDao;
import util.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.UUID;

/**
 * @ Auther leiHua
 * @ Date 2022/5/13 21:58
 */
public class CompetitionDaoImpl implements CompetitionDao {


    /**
     * 查询一条比赛项目信息
     *
     * @param id 比赛项目的id
     * @return 根据id查询到比赛项目
     */
    @Override
    public Competition select(String id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from competition where c_id=?");
            ps.setString(1, id);
            rs = ps.executeQuery();
            Competition competition = new Competition();
            if (rs.next()) {
                String place = rs.getString("place");
                String name = rs.getString("name");
                int number = rs.getInt("number");
                String time = rs.getString("time");
                competition.setC_id(id);
                competition.setCompetitionName(name);
                competition.setPalce(place);
                competition.setNumber(number);
                competition.setTime(time);
                return competition;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return null;
    }


    /**
     * 查询所有比赛项目信息
     *
     * @return 比赛项目类列表
     */
    public ArrayList<Competition> selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("select * from competition ");
            rs = ps.executeQuery();
            ArrayList<Competition> competitionList = new ArrayList<>();
            while (rs.next()) {
                Competition competition = new Competition();
                String c_id = rs.getString("c_id");
                String place = rs.getString("place");
                String name = rs.getString("name");
                int number = rs.getInt("number");
                String time = rs.getString("time");
                competition.setC_id(c_id);
                competition.setCompetitionName(name);
                competition.setPalce(place);
                competition.setNumber(number);
                competition.setTime(time);
                competitionList.add(competition);
            }
            return competitionList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return null;
    }

    /**
     * 添加一条比赛项目信息
     *
     * @param competition 前端传入一条比赛信息类
     * @return
     */
    @Override
    public boolean insert(Competition competition) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("insert into competition values(?,?,?,?,?)");
            ps.setString(1, competition.getC_id());
            ps.setString(2, competition.getCompetitionName());
            ps.setString(3, competition.getPalce());
            ps.setInt(4, competition.getNumber());
            ps.setString(5, competition.getTime());
            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }

        return false;
    }

    /**
     * @param competition 前端传入一条修改后的比赛信息类，前端界面不能修改和显示id值
     * @return 更改情况
     */
    @Override
    public boolean update(Competition competition) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            String c_id = competition.getC_id();
            ps = conn.prepareStatement("update competition set name=?,place=?,number=?,time=? where c_id=?");
            ps.setString(1, competition.getCompetitionName());
            ps.setString(2, competition.getPalce());
            ps.setInt(3, competition.getNumber());
            ps.setString(4, competition.getTime());
            ps.setString(5, c_id);
            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }

    public boolean add(Competition competition) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            String c_id = competition.getC_id();
            ps = conn.prepareStatement("insert into  competition  values(?,?,?,?,?)");
            ps.setString(1, c_id);
            ps.setString(2, competition.getCompetitionName());
            ps.setString(3, competition.getPalce());
            ps.setInt(4, competition.getNumber());
            ps.setString(5, competition.getTime());
            int i = ps.executeUpdate();
            if (i == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }

    public boolean delete(String id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement("delete  from  competition  where c_id=?");
            ps.setString(1, id);
            int count = ps.executeUpdate();
            if (count==1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn, ps, rs);
        }
        return false;
    }

    public static void main(String[] args) {
        CompetitionDaoImpl competitionDao = new CompetitionDaoImpl();
        //测试select()
//        System.out.println( competitionDao.select("1"));
//
//        //测试selecAll
//        ArrayList<Competition> arrayList = competitionDao.selectAll();
//        for (Competition competition : arrayList) {
//            System.out.println(competition.toString());
//        }
////        测试insert()
//        String id = UUID.randomUUID().toString();
//        Competition competition1 = new Competition(id,"男子1000米","操场",20,"2022-5-14");
//        System.out.println( competitionDao.insert(competition1));

        //测试 update
//        String id = UUID.randomUUID().toString();
//        Competition competition2 = new Competition(id, "测试update新增的项目", "操场", 20, "2022-5-14");
//        competitionDao.insert(competition2);
//        competition2.setNumber(16);
//        System.out.println(competitionDao.update(competition2));
        competitionDao.delete("100");


    }
}
